package site.yunnong.atvris.service.impl;

import com.github.pagehelper.PageHelper;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;
import site.yunnong.atvris.common.api.Page;
import site.yunnong.atvris.common.api.ResultCodeEnum;
import site.yunnong.atvris.common.dto.VideoQueryParam;
import site.yunnong.atvris.common.enumeration.MediaStatusEnum;
import site.yunnong.atvris.common.exception.YunnongBusinessException;
import site.yunnong.atvris.dao.domain.Video;
import site.yunnong.atvris.dao.domain.VideoExample;
import site.yunnong.atvris.dao.domain.ext.VideoDetail;
import site.yunnong.atvris.dao.mapper.VideoMapper;
import site.yunnong.atvris.dao.mapper.ext.VideoExtMapper;
import site.yunnong.atvris.service.MediaPartitionService;
import site.yunnong.atvris.service.VideoService;
import site.yunnong.atvris.service.helper.BuildHelper;
import site.yunnong.atvris.service.vo.VideoCardVO;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;
import java.util.Random;
import java.util.stream.Collectors;

/**
 * @author zhangjinghao
 * @email jhzhang9102@163.com
 * @date 2021-07-09 23:45:37
 */
@Slf4j
@Service
public class VideoServiceImpl implements VideoService {

    @Resource
    private VideoMapper videoMapper;
    @Resource
    private VideoExtMapper videoExtMapper;
    @Resource
    private MediaPartitionService mediaPartitionService;


    @Override
    public Video queryApprovedVideoByUvid(Long uvid) {
        VideoExample example = new VideoExample();
        example.createCriteria().andStatusEqualTo(MediaStatusEnum.APPROVED.getValue())
                .andUvidEqualTo(uvid);
        List<Video> videoList = videoMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(videoList)) {
            return null;
        }
        return videoList.get(0);
    }

    @Override
    public int insert(Video video) {
        return videoMapper.insertSelective(video);
    }

    @Override
    public int update(Video newVideo, VideoExample example) {
        return videoMapper.updateByExampleSelective(newVideo, example);
    }

    @Override
    public List<Video> selectRandomVideoList(Integer pageSize, Long pid) {
        VideoExample example = new VideoExample();
        VideoExample.Criteria criteria = example.createCriteria();
        criteria.andStatusEqualTo(MediaStatusEnum.APPROVED.getValue());
        if (pid != null) {
            List<Long> pidList = mediaPartitionService.queryChildPidList(pid);
            criteria.andPidIn(pidList);
        }
        long count = videoMapper.countByExample(example);
        Integer pageNum = new Random().nextInt((int) count - 1) / pageSize + 1;
        PageHelper.startPage(pageNum, pageSize);
        return videoMapper.selectByExample(example);
    }

    @Override
    public List<Video> queryAll() {
        return videoMapper.selectByExample(new VideoExample());
    }

    @Override
    public Page queryUserVideoPage(VideoQueryParam param) {
        VideoExample example = new VideoExample();
        VideoExample.Criteria criteria = example.createCriteria();
        if (param.getUuid() != null) {
            criteria.andUuidEqualTo(param.getUuid());
        } else {
            throw new YunnongBusinessException(ResultCodeEnum.ERROR_PARAM_ILLEGAL);
        }
        if (param.getPid() != null) {
            criteria.andPidEqualTo(param.getPid());
        }
        if (!MediaStatusEnum.ALL.getValue().equals(param.getStatus())) {
            criteria.andStatusEqualTo(param.getStatus());
        }
        long count = videoMapper.countByExample(example);
        List<VideoDetail> videoDetailList = Lists.newArrayList();
        try {
            param.setStartIndex((param.getPageNum() -1) * param.getPageSize());
            videoDetailList = videoExtMapper.queryUserVideoList(param);
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        List<VideoCardVO> collect = Optional.of(videoDetailList).orElse(Lists.newArrayList())
                .stream().map(BuildHelper::buildVideoCardVO)
                .collect(Collectors.toList());
        return new Page(param.getPageNum(), param.getPageSize(), count, collect);
    }
}

